package server;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

public class Logger {
	private final static String DEFAULT_LOG_FILENAME = "log.txt";
	private File logFile;
	private FileWriter writer;

	// skapa konstanter för händelser

	public Logger() {
		logFile = new File(DEFAULT_LOG_FILENAME);
		try {
			writer = new FileWriter(logFile, true);
		} catch (IOException e) {
			System.err.println("Could not create writer.");
			e.printStackTrace();
		}
	}

	public Logger(String logFileName) {
		this.logFile = new File(logFileName);
		try {
			writer = new FileWriter(logFile);
		} catch (IOException e) {
			System.err.println("Could not create writer.");
			e.printStackTrace();
		}
	}

	/**
	 * Writes a message together with a timestamp to the log file.
	 * 
	 * @param msg
	 *            the message
	 */
	public void log(String msg) {
		try {
			writer.write((new Date()).toString() + " log message: " + msg
					+ "\n");
			writer.flush();
		} catch (IOException e) {
			System.err.println("Problems writing to log file.");
			e.printStackTrace();
		}
	}

	public static void main(String args[]) throws IOException {
		Logger l = new Logger();
		// Logger l = new Logger("logfil");
		l.log("hej");
		l.log("hej igen");
	}
}
